#include "registerdialog.h"
#include "ui_registerdialog.h"

#include<QRegExp>
#include<QValidator>
#include<QMessageBox>
#include<QTimer>
#include<QSqlQuery>
#include<QSqlDatabase>
#include<QCryptographicHash>
#include<QDebug>

RegisterDialog::RegisterDialog(QWidget *parent) :
    QDialog(parent, Qt::FramelessWindowHint),
    ui(new Ui::RegisterDialog)
{
    ui->setupUi(this);
    //初始化界面
    initializeStyleSheet();
    //关闭按钮
    connect(ui->closePushButton, &QAbstractButton::clicked,
            this, [&](){

        this->close();
    });
    //取消按钮
    connect(ui->cancelPushButton, &QAbstractButton::clicked,
            this, [&](){
        this->close();
    });
    //注册操作
    connect(ui->savePushButton, &QAbstractButton::clicked,
            this, &RegisterDialog::registerOperate);


}

RegisterDialog::~RegisterDialog()
{
    delete ui;
}

void RegisterDialog::initializeStyleSheet()
{
    this->setStyleSheet("QDialog#RegisterDialog{background-color:lightGray;color:balck}");
    //关闭
    ui->cancelPushButton->setStyleSheet("QPushButton#cancelPushButton{background:rgba(0,0,0,0);border:1px solid rgba(0,0,0,0)}");
    //关闭按钮
    ui->closePushButton->setStyleSheet("QPushButton{border-image: url(:/Image/close.png)}");
    //保存
     ui->savePushButton->setIcon(QIcon(":/Image/save.png"));
     ui->savePushButton->setIconSize(QSize(ui->savePushButton->width(), ui->savePushButton->height()));
     ui->savePushButton->setStyleSheet("QPushButton#savePushButton{background:rgba(0,0,0,0);border:1px solid rgba(0,0,0,0)}");
     //取消
     ui->cancelPushButton->setIcon(QIcon(":/Image/cancel.png"));
     ui->cancelPushButton->setIconSize(QSize(ui->cancelPushButton->width(), ui->cancelPushButton->height()));
     //原名字
     QRegExp passwordRegx("[a-zA-Z0-9]+$");
     //新名字
     QValidator* accountValidator = new QRegExpValidator(passwordRegx, ui->accountLineEdit);
     ui->accountLineEdit->setValidator(accountValidator);
     ui->accountLineEdit->setMaxLength(20);
     //新名字
     QValidator* passwordValidator2 = new QRegExpValidator(passwordRegx, ui->passwordLineEdit);
     ui->passwordLineEdit->setValidator(passwordValidator2);
     ui->passwordLineEdit->setMaxLength(20);
     //确认新名字
     QValidator* passwordValidator3 = new QRegExpValidator(passwordRegx, ui->affirmPasswordLineEdit);
     ui->affirmPasswordLineEdit->setValidator(passwordValidator3);
     ui->affirmPasswordLineEdit->setMaxLength(20);
     //密文输入
     ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
     ui->affirmPasswordLineEdit->setEchoMode(QLineEdit::Password);
     ui->accountLineEdit->setEchoMode(QLineEdit::Password);
     //改变字体显示颜色
     ui->accountLabel->setStyleSheet("color:black");
     ui->accountLineEdit->setStyleSheet("color:black");
     ui->passwordLabel->setStyleSheet("color:black");
     ui->passwordLineEdit->setStyleSheet("color:black");
     ui->affirmPasswordLabel->setStyleSheet("color:black");
     ui->affirmPasswordLineEdit->setStyleSheet("color:black");
}

void RegisterDialog::registerOperate()
{
    QString account = ui->accountLineEdit->text();
    QString password = ui->passwordLineEdit->text();
    QString affrimPassword = ui->affirmPasswordLineEdit->text();
    if(account.size() == 0){
        QMessageBox* infoBox = new QMessageBox(QMessageBox::Critical, tr("错误信息"), tr("账户为空，请重新输入！"));
        QTimer::singleShot(1500, infoBox, &QMessageBox::deleteLater);
        infoBox->show();
        return;
    }
    if(password.size() == 0){
        QMessageBox* infoBox = new QMessageBox(QMessageBox::Critical, tr("错误信息"), tr("密码为空，请重新输入！"));
        QTimer::singleShot(1500, infoBox, &QMessageBox::deleteLater);
        infoBox->show();
        return;
    }
    if(affrimPassword.size() == 0){
        QMessageBox* infoBox = new QMessageBox(QMessageBox::Critical, tr("错误信息"), tr("确认密码为空，请重新输入！"));
        QTimer::singleShot(1500, infoBox, &QMessageBox::deleteLater);
        infoBox->show();
        return;
    }
    //判密码一致性
    qDebug()<<"affrimPassword:"<<affrimPassword;
    qDebug()<<"password:"<<password;
    if(affrimPassword != password){
        ui->passwordLineEdit->clear();
        ui->affirmPasswordLineEdit->clear();
        ui->passwordLineEdit->setFocus();
        QMessageBox* infoBox = new QMessageBox(QMessageBox::Critical, tr("错误信息"), tr("新密码和确认密码不一致，请重新输入！"));
        QTimer::singleShot(1500, infoBox, &QMessageBox::deleteLater);
        infoBox->show();
        return;
    }
    QSqlQuery query(QSqlDatabase::database("connection1"));
    query.exec(QString("SELECT COUNT(username) FROM UserInformation WHERE username = \'%1\'").arg(account));
    qDebug()<<"sql:"<<QString("SELECT COUNT(username) FROM UserInformation WHERE username = \'%1\'").arg(account);
    while(query.next()){
        qDebug()<<"query.value(0).toString():"<<query.value(0).toString();
        if(query.value(0).toInt() != 0){
            QMessageBox* infoBox = new QMessageBox(QMessageBox::Critical, tr("错误信息"), tr("该用户名已被注册，请重新输入！"));
            QTimer::singleShot(1500, infoBox, &QMessageBox::deleteLater);
            infoBox->show();
            return;
        }
    }
    QCryptographicHash passwordHash(QCryptographicHash::Md5);
    QByteArray passwordMD5;
    passwordMD5.append(password.toUtf8());
    passwordHash.addData(passwordMD5);
    password = passwordHash.result().toHex();

    query.exec(QString("INSERT INTO UserInformation(username,password) VALUES (\'%1\', \'%2\')").arg(account).arg(password));
    qDebug()<<"sql:"<<QString("INSERT INTO UserInformation(username,password) VALUES (\'%1\', \'%2\')").arg(account).arg(password);
    QMessageBox* infoBox = new QMessageBox(QMessageBox::Information, tr("信息提示"), tr("新用户注册成功，请重新登陆！"));
    QTimer::singleShot(1500, infoBox, &QMessageBox::deleteLater);
    infoBox->show();

    close();
}

void RegisterDialog::initializeInterface()
{
    this->show();
}
